﻿using KinonekoSoftware.CodariaDB.Documents.Timelines;

namespace KinonekoSoftware.Studio.Pages.Notions
{
    public sealed partial class PlotsQuickEditorViewModel : RepositoryGalleryItem
    {

        //-------------------------------------------------------------
        //
        //          Fields
        //
        //-------------------------------------------------------------
        private readonly TimeSystem     _system;
        private readonly TimelineSystem _timelineSystem;

        private Plot _plot;


        //-------------------------------------------------------------
        //
        //          Constructor
        //
        //-------------------------------------------------------------
        public PlotsQuickEditorViewModel()
        {
            _system = Platform.GetEngine<TimeSystem>();

            //
            //
            _timelineSystem = new TimelineSystem(_system);

            _timelineSystem.OnPlotChanged = OnPlotChanged;

            SaveCommand = CreateCommand(DoSaveCommand);

            AddPlotCommand      = CreateCommand(DoAddPlotCommand);
            AttachPlotCommand   = CreateCommand<StoryPoint>(DoAttachPlotCommand);
            CreateBranchCommand = CreateCommand(DoCreateBranchCommand);
            EditPlotCommand     = CreateCommand<Plot>(DoEditPlotCommand);
            RemovePlotCommand   = CreateCommand<Plot>(DoRemovePlotCommand);

            AddStoryPointCommand       = CreateCommand(DoAddStoryPointCommand);
            EditStoryPointCommand      = CreateCommand<StoryPoint>(DoEditStoryPointCommand);
            RefineStoryPointCommand    = CreateCommand<StoryPoint>(DoRefineStoryPointCommand);
            AttachStoryPointCommand    = CreateCommand<StoryPoint>(DoAttachStoryPointCommand);
            DetachStoryPointCommand    = CreateCommand<StoryPoint>(DoDetachStoryPointCommand);
            ShiftUpStoryPointCommand   = CreateCommand<StoryPoint>(DoShiftUpStoryPointCommand);
            ShiftDownStoryPointCommand = CreateCommand<StoryPoint>(DoShiftDownStoryPointCommand);
            RemoveStoryPointCommand    = CreateCommand<StoryPoint>(DoRemoveStoryPointCommand);

            PopAllLayerCommand = CreateCommand(DoPopAllLayerCommand);
            PopLayerCommand    = CreateCommand(DoPopLayerCommand);
            GotoLayerCommand   = CreateCommand<StoryPoint>(DoGotoLayerCommand);

            EnableSafetyExit = true;
        }

        private void OnPlotChanged(Plot value)
        {
            Plot = value;
        }


        //-------------------------------------------------------------
        //
        //          Private Methods
        //
        //-------------------------------------------------------------


        //-------------------------------------------------------------
        //
        //          Override Methods
        //
        //-------------------------------------------------------------
        protected override void OnStart()
        {

            //
            // 添加所有的
            _timelineSystem.RebuildPlots();
            _timelineSystem.RebuildDetachStoryPoint();


            base.OnStart();
        }

        protected override void OnSuspend()
        {
            //
            // 取消订阅
            _timelineSystem.OnPlotChanged = null;

            if (IsChange)
            {
                Save();
            }

            base.OnSuspend();
        }


        //-------------------------------------------------------------
        //
        //          Public Methods
        //
        //-------------------------------------------------------------

        public void Save()
        {
            //
            //
            _timelineSystem.SavePlotChanges();

            //
            //
            SetDirtyState(false);
        }

        //-------------------------------------------------------------
        //
        //          Command Handlers
        //
        //-------------------------------------------------------------

        private async void DoSaveCommand()
        {
            //
            //
            Save();

            //
            //
            await Success(TitleSR.SuccessOperation, TextSR.Successful_Save);
        }




        //-------------------------------------------------------------
        //
        //          Properties
        //
        //-------------------------------------------------------------

        public Plot Plot
        {
            get => _plot;
            set
            {
                SetValue(ref _plot, value);
                _timelineSystem.Plot = value;
                _timelineSystem.RebuildStoryPoint(value);
                RefreshStoryPointState();
            }
        }

        public ObservableCollection<Plot> PlotCollection => _timelineSystem.PlotCollection;




        //-------------------------------------------------------------
        //
        //          Commands
        //
        //-------------------------------------------------------------

        public ICommandEX SaveCommand { get; }
    }
}